
package Visual;

import Excepciones.YaExiste;
import Logica.Ingreso;
import Logica.MateriaPrima;
import Logica.Parcela;
import Logica.Plantacion;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import javax.swing.ListModel;


public class frmAltaPlantacion extends javax.swing.JDialog {

    public void ObtenerParcela(Parcela miParcela) {
        this.listaParcelas.addElement(miParcela); //To change body of generated methods, choose Tools | Templates.
    }
    private Principal unaPrincipal = new Principal();
    private ControladoraVisual miControladora = new ControladoraVisual();
    private DefaultListModel listaIngresos = new DefaultListModel();
    private DefaultListModel listaParcelas = new DefaultListModel();
    boolean habilitar = true;
    frmAltaIngreso UnoIngreso=null;
    frmAltaAplicacion unaAplicacion=null;
    
    public frmAltaPlantacion(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        setTitle("Alta Plantación");
        initComponents();
        this.lstIngresos.setModel(listaIngresos);
        this.lstParcelas.setModel(listaParcelas);
        this.setLocationRelativeTo(unaPrincipal);
        List<Ingreso> ings = miControladora.obtenerListIngreso();
        List<Parcela> parc;
        this.agregarIngreso(ings);
    }
    
    public frmAltaPlantacion(java.awt.Frame parent, boolean modal,boolean habilitar, frmAltaIngreso unaVentana) {
        super(parent, modal);
        setTitle("Alta Plantación");
        initComponents();
        this.lstIngresos.setModel(listaIngresos);
        this.lstParcelas.setModel(listaParcelas);
        this.setLocationRelativeTo(unaPrincipal);
        List<Ingreso> ings = miControladora.obtenerListIngreso();
        List<Parcela> parc;
        lstIngresos.setEnabled(habilitar);
        this.habilitar = habilitar;
        this.UnoIngreso = unaVentana;
        
    }
    
    public frmAltaPlantacion(java.awt.Frame parent, boolean modal,boolean habilitar, frmAltaAplicacion unaVentana) {
        super(parent, modal);
        setTitle("Alta Plantación");
        initComponents();
        this.lstIngresos.setModel(listaIngresos);
        this.lstParcelas.setModel(listaParcelas);
        this.setLocationRelativeTo(unaPrincipal);
        List<Ingreso> ings = miControladora.obtenerListIngreso();
        List<Parcela> parc;
        lstIngresos.setEnabled(habilitar);
        this.habilitar = habilitar;
        this.unaAplicacion = unaVentana;
        
    }
  
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblPropietario = new javax.swing.JLabel();
        txtPropietario = new javax.swing.JTextField();
        lblUbicacion = new javax.swing.JLabel();
        txtUbicacion = new javax.swing.JTextField();
        lblTipo = new javax.swing.JLabel();
        cmbTipo = new javax.swing.JComboBox();
        lblSuperficie = new javax.swing.JLabel();
        txtSuperficie = new javax.swing.JTextField();
        lblParcelas = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        lstParcelas = new javax.swing.JList();
        btnAgregarParcela = new javax.swing.JButton();
        btnAlta = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        lstIngresos = new javax.swing.JList();
        jLabel2 = new javax.swing.JLabel();
        btnAgregarIngreso = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        lblPropietario.setText("Propietario");

        txtPropietario.setText("                   ");

        lblUbicacion.setText("Ubicación");

        txtUbicacion.setText("                  ");

        lblTipo.setText("Tipo");

        cmbTipo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Propias", "Alquiladas", "Terceros" }));

        lblSuperficie.setText("Superficie");

        txtSuperficie.setText("                       ");

        lblParcelas.setText("Parcelas");

        lstParcelas.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "No hay parcelas." };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane1.setViewportView(lstParcelas);

        btnAgregarParcela.setText("Agregar Parcela");
        btnAgregarParcela.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAgregarParcelaActionPerformed(evt);
            }
        });

        btnAlta.setText("Alta");
        btnAlta.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAltaActionPerformed(evt);
            }
        });

        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        jScrollPane3.setViewportView(lstIngresos);

        jLabel2.setText("Ingreso");

        btnAgregarIngreso.setText("Agregar Ingreso");
        btnAgregarIngreso.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAgregarIngresoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lblSuperficie)
                        .addGap(18, 18, 18)
                        .addComponent(txtSuperficie, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lblPropietario)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txtPropietario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblUbicacion)
                            .addComponent(lblTipo))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtUbicacion)
                            .addComponent(cmbTipo, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnAgregarParcela))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(18, 18, 18)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(35, 35, 35)
                            .addComponent(lblParcelas))))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(56, 56, 56)
                        .addComponent(jLabel2))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                            .addComponent(btnAgregarIngreso, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap(41, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btnAlta)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnCancelar)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(lblPropietario)
                        .addComponent(txtPropietario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(lblParcelas))
                    .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblUbicacion)
                            .addComponent(txtUbicacion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblTipo)
                            .addComponent(cmbTipo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblSuperficie)
                            .addComponent(txtSuperficie, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnAgregarParcela)
                            .addComponent(btnAgregarIngreso))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnCancelar)
                            .addComponent(btnAlta))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        dispose();
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void btnAgregarParcelaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAgregarParcelaActionPerformed
        frmAltaParcela unaVentana = new frmAltaParcela(unaPrincipal,true,false,this);
        unaVentana.setVisible(true);
        
        
    }//GEN-LAST:event_btnAgregarParcelaActionPerformed

    private void btnAltaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAltaActionPerformed
        // TODO add your handling code here:
        this.altaPlantacion();
        this.inicializarTextFields();
    }//GEN-LAST:event_btnAltaActionPerformed

    private void btnAgregarIngresoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAgregarIngresoActionPerformed
        // TODO add your handling code here:
        frmAltaIngreso unaVentana = new frmAltaIngreso(unaPrincipal,true,false,this);
        unaVentana.setVisible(true);
        
    }//GEN-LAST:event_btnAgregarIngresoActionPerformed

    private void altaPlantacion(){
        String propietario = this.txtPropietario.getText();
        String ubicacion = this.txtUbicacion.getText();
        String tipo = this.cmbTipo.getActionCommand();
        String superficie = this.txtSuperficie.getText();
        List<Parcela> parcelas; List<MateriaPrima> MP; List<Ingreso> ingresos;
        parcelas = this.lstParcelas.getSelectedValuesList();
        ingresos = this.lstIngresos.getSelectedValuesList();
        try {
            Plantacion unaPlantacion = miControladora.altaPlantacion(propietario,ubicacion,tipo,superficie,parcelas,ingresos);
            JOptionPane.showMessageDialog(this,"La operación ha sido realizada con éxito",":)",JOptionPane.INFORMATION_MESSAGE);
            if(!habilitar){
                if(!UnoIngreso.equals(null)){
                    UnoIngreso.btnAgregarPlantacion(unaPlantacion);
            }else
                    unaAplicacion.btnAgregarPlantacion(unaPlantacion);
        }
        }catch(YaExiste a){
            JOptionPane.showMessageDialog(this, a.getMessage(), "Lo sentimos, algo ha salido mal", JOptionPane.WARNING_MESSAGE);
        }
    }
    
    private void agregarIngreso(List<Ingreso> ings){
        for(Ingreso miIngreso:ings){
            listaIngresos.addElement(miIngreso);
        }
    }
    
     private void inicializarTextFields(){
            this.txtPropietario.setText("");
            this.txtSuperficie.setText("");
            this.txtUbicacion.setText("");
            this.lstParcelas.clearSelection();
            this.lstIngresos.clearSelection();
     }  
            
     
    public void mainMetodo() {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        /*try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(frmAltaPlantacion.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(frmAltaPlantacion.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(frmAltaPlantacion.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(frmAltaPlantacion.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }*/
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                frmAltaPlantacion dialog = new frmAltaPlantacion(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAgregarIngreso;
    private javax.swing.JButton btnAgregarParcela;
    private javax.swing.JButton btnAlta;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JComboBox cmbTipo;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JLabel lblParcelas;
    private javax.swing.JLabel lblPropietario;
    private javax.swing.JLabel lblSuperficie;
    private javax.swing.JLabel lblTipo;
    private javax.swing.JLabel lblUbicacion;
    private javax.swing.JList lstIngresos;
    private javax.swing.JList lstParcelas;
    private javax.swing.JTextField txtPropietario;
    private javax.swing.JTextField txtSuperficie;
    private javax.swing.JTextField txtUbicacion;
    // End of variables declaration//GEN-END:variables

    void ObtenerIngreso(Ingreso unIngreso) {
        listaIngresos.addElement(unIngreso); //To change body of generated methods, choose Tools | Templates.
    }
}
